package com.igoal.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.igoal.dto.UserDTO;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.StringUtils;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.UserPublicProfileModel;
import com.igoal.web.model.UserWallModel;

@Controller
@RequestMapping("/wall.html")
public class UserWallController extends BaseController {

	private boolean myWall = false;
	private String wallOwner;
	private UserDTO wallUser;

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showUserWall(ModelMap model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		wallOwner = request.getParameter("username");
		if (StringUtils.isBlank(wallOwner)) {
			wallOwner = getUserInSession();
		}

		if (wallOwner.equals(getUserInSession())) {
			myWall = true;
			wallUser = user;
		} else {
			myWall = false;
			wallUser = userManagerExt.getUserByUsername(wallOwner);
		}

		initModel(model, user, request);

		return getViewPrefix() + "userWall";
	}

	private void initModel(ModelMap modelMap, UserDTO user, HttpServletRequest request) {
		if (modelMap == null || user == null) {
			return;
		}

		if (wallUser == null) {
			return;
		}

		UserWallModel model = new UserWallModel();
		initTopPage(model, request);
		initRightPage(model, request);
		initLeftPage(model, request);
		model.setWallOwner(wallOwner);
		model.setWallOwnerName(wallUser.getFirstName());

		applyModel(modelMap, model);
	}

	public void initTopPage(BaseModel baseModel, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, baseModel);
		baseModel.setUserId(user.getId());
		
		if (!myWall) {
			UserDTO sessionUser = getUserSession();
			UserDTO publicUser = wallUser;

			UserWallModel model = (UserWallModel) baseModel;
			UserPublicProfileModel userModel = model.getUserModel();
			if (userModel == null) {
				userModel = new UserPublicProfileModel();
				model.setUserModel(userModel);
			}

			initUserAvatar(publicUser, userModel);
			initUserStatus(publicUser, userModel);
			initSessionInformation(userModel);
			userModel.setUsername(publicUser.getUsername());
			userModel.setUserId(publicUser.getId());

			if (sessionUser != null && !sessionUser.getUsername().equals(publicUser.getUsername())) {
				userModel.setFollowed(userManagerExt.isFriend(sessionUser, publicUser));
				userModel.setPendingAcceptFollow(userManagerExt.isWaitingAcceptFriend(sessionUser,
						publicUser));

				if (userModel.getUserStatus() != null
						&& userModel.getUserStatus().getUserStatusId() != null) {
					Boolean thumbStatus = userManagerExt.getUserThumbedStatus(
							sessionUser.getUsername(), userModel.getUserStatus().getUserStatusId());
					if (thumbStatus != null) {
						userModel.getUserStatus().setThumbStatus(thumbStatus);
					}
				}
			} else {
				userModel.setFollowed(false);
			}
		}
	}

	public void initRightPage(BaseModel baseModel, HttpServletRequest request) {
		if (myWall) {
			UserDTO user = getUserSession();
			initRightZone(user, baseModel);
			initPeopleYouMayKnow(user, baseModel);
		} else {
			UserDTO sessionUser = getUserSession();
			UserDTO publicUser = wallUser;

			UserWallModel model = (UserWallModel) baseModel;
			UserPublicProfileModel userModel = model.getUserModel();
			if (userModel == null) {
				userModel = new UserPublicProfileModel();
				model.setUserModel(userModel);
			}

			if (sessionUser != null && !sessionUser.getUsername().equals(publicUser.getUsername())) {
				initMutualFriends(userManagerExt, sessionUser, publicUser, model);
				initPeopleYouMayKnow(sessionUser, model);
			}
			initInformationBox(publicUser, userModel);
		}
	}

	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {
		UserWallModel model = (UserWallModel) baseModel;
		model.setWallActivities(actionLoggerService.getWallActivities(getUserInSession(),
				wallOwner, null));
	}

}
